<template>
  <div class="easy-code-card">
    <slot></slot>
    <p class="easy-code-card-des">{{ description }}</p>
    <div class="easy-code-card-showCode">
      <span class="easy-code-card_img">
        <img
          style="width: 16px; height: 16px"
          src="https://gw.alipayobjects.com/zos/antfincdn/Z5c7kzvi30/expand.svg"
          alt="expand code"
          v-if="!showCode"
          @click="showCode = true"
        />
        <img
          style="width: 16px; height: 16px"
          src="https://gw.alipayobjects.com/zos/antfincdn/4zAaozCvUH/unexpand.svg"
          alt="expand code"
          v-else
          @click="showCode = false"
        />
      </span>
    </div>
    <div v-if="showCode" class="easy-code-card-block">
      <pre>
        <code>{{ code }}</code>
      </pre>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'

export default defineComponent({
  name: 'CodeCard',
  props: {
    description: {
      type: String,
      required: true,
    },
    code: {
      type: String,
      required: true,
    },
  },
  setup() {
    const showCode = ref(false)
    const open = ref('<>')
    const close = ref('</>')
    return {
      open,
      close,
      showCode,
    }
  },
})
</script>

<style lang="scss">
.easy-code-card {
  border: 1px solid #f0f0f0;
  margin-bottom: 8px;
  &_img {
    width: 16px;
    height: 16px;
    color: rgba($color: #000000, $alpha: 0.45);
    cursor: pointer;
  }
  &-des {
    padding: 8px 16px;
    font-size: 14px;
    border-bottom: 1px solid #f0f0f0;
  }
  &-showCode {
    display: flex;
    justify-content: center;
    padding: 2px 8px;
    border-bottom: 1px solid #f0f0f0;
  }
}
</style>
